#include "sockettransport.h"

SocketTransport::SocketTransport(QTcpSocket* socket, MainExeqtor* mainExeqtor)
{
    this->socket = socket;
    this->mainExeqtor = mainExeqtor;
    this->receivedData = new QDataStream();
}

SocketTransport::~SocketTransport()
{
    delete this->receivedData;
}

void SocketTransport::Receive()
{
    while (true)
    {
        QString xml = "";
        QDomDocument doc;
        if (socket->state() == socket->ConnectedState)
        {
            while(socket->canReadLine()) {
                QString line = QString::fromUtf8(socket->readLine()).trimmed();
                xml.append(line);
            }
            if (xml != "")
            {
                doc.setContent(xml);
                Para* cmd = xmlparser::Parse(doc);
                mainExeqtor->executeCurrent(cmd);
            }
        }
    }
}

void SocketTransport::Send(QDomDocument command)
{
    QByteArray data = command.toByteArray();
    if (data.size() > 0 )
        socket->write(data);
}
